package top.drinyeit.weilong.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.citywheel.CityPickerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.base.BaseActivity;
import top.drinyeit.weilong.model.AddressDetailJsonBean;
import top.drinyeit.weilong.model.AuthenticationDetailBean;
import top.drinyeit.weilong.presenter.EditCompanyPresenter;
import top.drinyeit.weilong.presenter.EnterpriseCertificationPresenter;
import top.drinyeit.weilong.util.SPUtil;

/**
 * Created by bigHead on 2017/11/21.
 */

public class EnterpriseCertificationActivity extends BaseActivity {
    CityPickerView cityPicker;
    private RelativeLayout phone, company_name, number, place, IDcard, business_license;
    public TextView phone_show, company_name_show, number_show, place_show;
    public AuthenticationDetailBean authenticationDetailBean;

    private EnterpriseCertificationPresenter enterpriseCertificationPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_enterprise_certification);

        initView();
        initData();
        init();
        setTitle(true,"企业认证");
    }

    private void initView() {
        phone = findViewById(R.id.phone);
        company_name = findViewById(R.id.company_name);
        number = findViewById(R.id.number);
        place = findViewById(R.id.place);
        IDcard = findViewById(R.id.IDcard);
        business_license = findViewById(R.id.business_license);
        phone_show = findViewById(R.id.phone_show);
        company_name_show = findViewById(R.id.company_name_show);
        number_show = findViewById(R.id.number_show);
        place_show = findViewById(R.id.place_show);

    }

    private void initData() {
        enterpriseCertificationPresenter = new EnterpriseCertificationPresenter(this);
        enterpriseCertificationPresenter.getCompany();
    }

    @Override
    public void init() {
        //上传身份证
        IDcard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EnterpriseCertificationActivity.this, UploadIdcardActivity.class);
                startActivityForResult(intent,1);
            }
        });
        //上传营业执照
        business_license.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EnterpriseCertificationActivity.this, UploadLicenseActivity.class);
                startActivityForResult(intent,1);
            }
        });
        //手机号
        phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EnterpriseCertificationActivity.this, PhoneActivity.class);
                intent.putExtra("a", authenticationDetailBean);
                startActivityForResult(intent,1);
            }
        });
        //企业名称
        company_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EnterpriseCertificationActivity.this, CompanyNameActivity.class);
                intent.putExtra("a", authenticationDetailBean);
                startActivityForResult(intent,1);
            }
        });
        //营业执照号码
        number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EnterpriseCertificationActivity.this, NumberActivity.class);
                intent.putExtra("a", authenticationDetailBean);
                startActivityForResult(intent,1);
            }
        });
        place.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    if (imm.isActive()) {
                        imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                if (cityPicker == null) {
                    CityConfig cityConfig = new CityConfig.Builder(EnterpriseCertificationActivity.this)
                            .textSize(18)//（滚轮文字的大小，int 类型，默认为18）
                            .title("地区选择")//（选择器标题，默认为“选择地区”）
                            .titleBackgroundColor("#ffffff")//（标题栏背景，默认为灰色，#C7C7C7）
                            .titleTextColor("#333333")//(标题文字颜色，默认为 #E9E9E9)
                            .confirTextColor("#666666")//（确认按钮字体颜色，默认为系统的colorPrimary颜色值）
                            .cancelTextColor("#666666")//（取消按钮字体颜色，默认为系统的colorPrimary颜色值）
                            .province("浙江")//（默认的显示省份，显示选择器后直接定位的item位置）
                            .city("温州")//（默认的显示市，显示选择器后直接定位的item位置）
                            .district("苍南县")//（默认的显示区，显示选择器后直接定位的item位置）
                            .textColor("#333333")//（滚轮文字的颜色 ，int 类型，默认为0xFF585858）
                            .provinceCyclic(true)//（省份的滚轮是否循环滚动）
                            .cityCyclic(false)//（市的滚轮是否循环滚动）
                            .districtCyclic(false)//（区的滚轮是否循环滚动）
                            .visibleItemsCount(7)//（滚轮显示的item个数，int 类型，默认为5个）
                            .itemPadding(10)//（滚轮item间距，默认为5dp）
                            .setCityInfoType(CityConfig.CityInfoType.BASE) //BASE:只包含省市区名称; DETAIL:包含详细的经纬度、code、省市区数据名称等数据
                            .setCityWheelType(CityConfig.WheelType.PRO_CITY_DIS)
                            .build();
                    cityPicker = new CityPickerView(cityConfig);
                    //监听方法，获取地址选择结果
                    cityPicker.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
                        @Override
                        public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                            authenticationDetailBean.getCompany_address_json().setProvince(province.getName());
                            authenticationDetailBean.getCompany_address_json().setCity(city.getName() + "市");
                            authenticationDetailBean.getCompany_address_json().setRegion(district == null ? "" : district.getName());
                            Map<String,Object> map = new HashMap<>();
                            map.put("action","company_authentication");
                            map.put("user_id", SPUtil.getShareString(Constant.USER_ID));
                            map.put("company_address",new Gson().toJson(authenticationDetailBean.getCompany_address_json()));
                            enterpriseCertificationPresenter.editCompany(map, province,city,district);
                        }

                        @Override
                        public void onCancel() {

                        }
                    });
                }
                cityPicker.show();
            }
        });
    }

    public void setAuthenticationDetailBean(AuthenticationDetailBean authenticationDetailBean) {
        this.authenticationDetailBean = authenticationDetailBean;
    }

    public TextView getPlace_show() {
        return place_show;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1){
            initData();
        }
    }
}
